 
import {
  Application,
  FrontendOrBackendSession,
  HandlerCallback,
  IHandlerFilter,
  RESERVED,
  RouteRecord
} from 'pinus';
import net_result from '../consts/net_result';
import { PlayerService } from '../services/player_service';
import { Player } from '../entity/player.entity';

 
export class ValidatePlayerFilter implements IHandlerFilter {
  

  public before (routeRecord: RouteRecord, msg: any, session: FrontendOrBackendSession, next: HandlerCallback){
    if(!session.get("pid")){
        return next(new Error(net_result.msg.NOT_ROLE),net_result.error(net_result.code.ERROR_NOT_ROLE,net_result.msg.NOT_ROLE));
    }

    let player:Player=PlayerService.ins().getPlayer(session.get("pid"));
    if(!player){
        return next(new Error(net_result.msg.NOT_ROLE),net_result.error(net_result.code.ERROR_LIST_NOT_ROLE,net_result.msg.NOT_ROLE));
    }
    msg?msg.player=player:msg["player"]=player;
    return next(null,msg);
  }

  public after (err: Error, routeRecord: RouteRecord, msg: any, session: FrontendOrBackendSession, resp: any, next: HandlerCallback){
    if(err){
        if(resp){
            return next(null,resp);
        }
        else{
            return next(err);
        }
    }
    return next(null,resp);
  }

 
}

export default function () {
  return new ValidatePlayerFilter();
}